import pandas as pd
import numpy as np
import random
import statsmodels.api as sm

bm = pd.read_excel('bm_data.xls')
ret = pd.read_excel('ret_data.xls')

divPos=241

def divOldNew(data):
    return data.iloc[:divPos,:],data.iloc[divPos:,:]

def sample(x,y):
    sampleX=[]
    sampleY=[]
    len,_=x.shape
    for _ in range(40):
        sub=random.randint(0,len-1)
        sampleX.append(x.ix[sub,])
        sampleY.append(y.ix[sub,])
    sampleX=pd.DataFrame(sampleX)
    sampleY=pd.DataFrame(sampleY)
    return sampleX,sampleY

def toNp(data):
    data = np.array(data)[:, 1:]
    data = np.array(data, dtype=np.float32)
    return data

oldbm,newbm=divOldNew(bm)
oldret,newret=divOldNew(ret)

oldbmNp=toNp(oldbm)
oldretNp=toNp(oldret)

oldbm50=oldbmNp[:,-50:]
oldret50=oldretNp[:,-50:]

oldbm6=oldbmNp[:,:6]
oldret6=oldretNp[:,:6]

newbm=toNp(newbm)
retNp=toNp(ret)

newbm6=newbm[:,:6]
newbm50=newbm[:,-50:]
ret6=retNp[:,:6]
ret50=retNp[:,-50:]

def var(abm,aret):
    model=sm.tsa.VARMAX(aret,abm,order=(1,0))
    fitResult=model.fit(maxiter=100)
    print(fitResult.summary())
    return fitResult

def caluR2(true,pred):
    allR2=[] # 每个特征的R2值
    _,featureNum=pred.shape
    for i in range(featureNum):
        predi=pred[:,i]
        truei=true[:,i]
        delta=predi-truei
        score=1-delta.var()/truei.var()
        allR2.append(score)
    return allR2

def getAllR2(true,pred): # 返回顺序为：全样本的、IS、OSS的R2
    result=[caluR2(true,pred), caluR2(true[:divPos],pred[:divPos]), caluR2(true[divPos:],pred[divPos:])]
    result=np.array(result) # 行为每种样本，列为每个特征。如第一行就是全样本各个特征的R2
    return result

# 前6列VAR
fit3=var(oldbm6,oldret6)
pred3=fit3.predict(0,479,exog=newbm6)
R2_3=getAllR2(ret6,pred3)
print(R2_3)

# 自抽样10000次
for _ in range(10000):
    sbm,sret=sample(oldbm,oldret)
    sbm=toNp(sbm)[:,:6]
    sret=toNp(sret)[:,:6]
    fit2=var(sbm,sret) # 现在是每次抽样都搞了一个模型

# ret AR（不支持多变量）
AR=sm.tsa.ARMA(oldret,order=(1,0))
ARfit=AR.fit(1)
ARpred=ARfit.predict(0,479)
R2_AR=getAllR2(ret,ARpred)
print(R2_AR)

# 后50列VAR
fit1=var(oldbm50,oldret50)
pred1=fit1.predict(0,479,exog=newbm50)
R2_1=getAllR2(ret50,pred1)
print(R2_1)
